<template>
	<view class="page-block">
		<cl-message ref="message"></cl-message>
		<view class="page-wrapper">
			<text class="login-title">科成拍卖平台登录</text>
			<view class="input-wrapper">
				<input type="text" placeholder="邮箱/手机号" maxlength="20" v-model="account" />
				<input type="text" password placeholder="密码" maxlength="16" v-model="password" />
				<button class="login-btn" @click="login">登&nbsp;&nbsp;录</button>
			</view>
			<navigator url="../register/register" class="link-reg">没有账号？注册一个吧</navigator>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				account: '',
				password: '',
				url: '',
				lotId: ''
			}
		},
		methods: {
			login() {
				if (this.account == "" || this.password == "") {
					this.$refs["message"].open({
						type: 'cancel',
						message: "账号或密码不能为空",
						duration: 2000,
						iconSize: 24
					});
					return
				}
				if (!RegExp(/^1[3456789]\d{9}$/).test(this.account) && !RegExp(/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/)
					.test(this.account)) {
					this.$refs["message"].open({
						type: 'cancel',
						message: "账号格式有误",
						duration: 2000,
						iconSize: 24
					});
					return
				}
				if (this.password.length < 6) {
					this.$refs["message"].open({
						type: 'cancel',
						message: "密码长度不足",
						duration: 2000,
						iconSize: 24
					});
					return
				}
				if (!RegExp(/(?!^(\d+|[a-zA-Z]+|[~!@#$%^&*?.]+)$)^[\w~!@#$%^&*?.]{6,16}$/).test(this.password)) {
					this.$refs["message"].open({
						type: 'cancel',
						message: "密码格式有误",
						duration: 2000,
						iconSize: 24
					});
					return
				}
				// 发起登录请求
				let user = {
					account: this.account,
					password: this.password
				};
				// 发起登录请求
				this.$common.request(this.$serverUrl + "login", user, "POST", this.successCallback, this.failCallback);
				this.$common.showLoading("登录中，清稍后...", true);
			},
			successCallback(res) {
				this.$common.hideLoading();
				if (res.data.msg == "ok") {
					let userData = res.data.data[0];
					let user = {
						uid: userData.uid,
						name: userData.name,
						phone: userData.phone,
						email: userData.email,
						gender: userData.gender,
						qq: userData.qq,
						photo: userData.photo == null ? "/static/icon/head-icon.jpg" : this.$common.photoUrlFormat(userData.photo)
					};
					this.$common.setUser(user);
					this.$common.setHasLogin(true)
					if (getCurrentPages().length <= 1) {
						uni.switchTab({
							url: "../mine/mine"
						})
					} else {
						if (this.url != null && this.url.length > 0) {
							if (this.url == "index" || this.url == "mine") {
								uni.switchTab({
									url: "../" + this.url + "/" + this.url
								})
							} else if (this.url == "add") {
								uni.navigateBack({
									delta: 1
								})
							} else if (this.lotId != null && this.lotId.length > 0) {
								uni.redirectTo({
									url: "/pages/" + this.url + "/" + this.url + "?lotId=" + this.lotId
								})
							} else {
								uni.redirectTo({
									url: "/pages/" + this.url + "/" + this.url
								})
							}
						} else {
							uni.navigateBack({
								delta: 1
							})
						}
					}
				} else {
					this.$common.showModal(res.data.msg, false);
				}
			},
			failCallback(res) {
				this.$common.hideLoading();
				this.$refs["message"].open({
					type: 'cancel',
					message: "登录失败，请稍后重试",
					duration: 2000,
					iconSize: 24
				});
				return
			}
		},
		onLoad(option) {
			this.url = option.url;
			this.lotId = option.lotId;
		},
		onBackPress() {
			if (getCurrentPages().length <= 1) {
				uni.switchTab({
					url: "../mine/mine"
				})
			} else {
				if (this.url != null && this.url.length > 0) {
					if (this.url == "index" || this.url == "mine") {
						uni.switchTab({
							url: "../" + this.url + "/" + this.url
						})
					} else if (this.url == "add") {
						uni.navigateBack({
							delta: 1
						})
					} else if (this.lotId != null && this.lotId.length > 0) {
						uni.redirectTo({
							url: "/pages/" + this.url + "/" + this.url + "?lotId=" + this.lotId
						})
					} else {
						uni.redirectTo({
							url: "/pages/" + this.url + "/" + this.url
						})
					}
				} else {
					uni.navigateBack({
						delta: 1
					})
				}
			}
			return true;
		}
	}
</script>

<style lang="scss">
	@import url("login.css");

	/deep/ .cl-message {
		&__content {
			font-size: 16px;
		}
	}
</style>
